package com.laxcen.smoke.service.impl;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.iot.model.v20180120.InvokeThingServiceRequest;
import com.aliyuncs.iot.model.v20180120.InvokeThingServiceResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.laxcen.smoke.dao.ScheduleDao;
import com.laxcen.smoke.util.DBUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 定时任务
 */
@Component
public class ScheduleImpl {

    @Autowired
    private ScheduleDao scheduleDao;

    @Value("${productkey}")
    private String productkey;


    //定时任务下发密码 每天零点执行
    //参数说明  [秒] [分] [小时] [日] [月] [周] [年]
    @Scheduled(cron = "0 0 0 * * ? ")
    public void clearDataJob() {
        System.out.println("密码定时下发执行");
        List<String> list = scheduleDao.getSmartNo();
        for (int i = 0;i < list.size();i++){
            String password = scheduleDao.getpasswordSchedule(list.get(i));
            IClientProfile profile = DefaultProfile.getProfile("cn-shanghai", "LTAI5tQqhoe1XSrkoCWkDDQ9", "MXycRe3SmNlDPKuLNt1cfxhFyjk5Cf");
            DefaultAcsClient client = new DefaultAcsClient(profile);
            InvokeThingServiceRequest request = new InvokeThingServiceRequest();
            String param = "{\"MessageId\":\""+ DBUtils.nextId()+"\",\"DeviceNo\":\""+list.get(i)+"\",\"Pwd\":\""+password+"\"}";
            request.setArgs(param);
            request.setIdentifier("UpdatePwd");
            request.setProductKey(productkey);
            request.setDeviceName(list.get(i));
            try {
                InvokeThingServiceResponse response = client.getAcsResponse(request);
                System.out.println(response.getSuccess());
                System.out.println(response.getErrorMessage());
            } catch (ServerException e) {
                e.printStackTrace();
            } catch (ClientException e) {
                e.printStackTrace();
            }

        }


    }

}
